package com.leo.biz.service.fetcher;
import com.leo.biz.po.DailyKRecord;
import com.leo.biz.service.pub.DaoCenter;
import com.leo.db.mapper.KrecordEverydayMapper;
import org.apache.commons.io.IOUtils;

import org.apache.http.HttpHost;
import org.apache.http.HttpRequest;
import org.apache.http.client.HttpClient;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicHttpRequest;
import org.apache.http.HttpResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.IOException;


/**
 * Created by leo on 2016/6/27.
 */
@Service("txStockFetcher") 
public class TxStockFetcher implements BaseStockFetcher {

    @Autowired
    private DaoCenter daoCenter;
    @Autowired
    private KrecordEverydayMapper krecordEverydayMapper;

    @Override
    public int getToadyStockInfo( String stockno) {
        int ret=0;
        String retStr=fetchHttp(stockno);
        if(retStr=="")
        {
            return  ret;
        }
        //System.out.println(retStr);
        String [] array= null;
        try {
            array = retStr.split("~");
            DailyKRecord temp=new DailyKRecord();
            temp.setStockno(stockno);
            temp.setCldate(Integer.valueOf(array[30].substring(0,8)));
            temp.setBegp(Float.valueOf(array[5]));
            temp.setEndp(Float.valueOf(array[3]));
            temp.setIncratio(Float.valueOf(array[32]));
            temp.setMaxp(Float.valueOf(array[33]));
            temp.setMinp(Float.valueOf(array[34]));
            temp.setMoney(Float.valueOf(array[37])*10000);/*单位:元*/
            temp.setChangerate(Float.valueOf(array[38]));
            temp.setVolume(Float.valueOf(array[36])*100);/*单位:股*/
            ret=daoCenter.insertResult(temp);
        } catch (Exception e) {
            e.printStackTrace();
            ret=0;
        }
        return ret;
    }

    public String fetchHttp(String stockno){
        String retstr="";
        String stockType="";
        if(stockno.startsWith("0"))
        {
            stockType="sz";
        }else
        {
            stockType="sh";
        }
        HttpClient httpClient= new DefaultHttpClient();
        HttpRequest req=new BasicHttpRequest("GET","http://qt.gtimg.cn/q="+stockType+stockno);
        HttpHost host=new HttpHost("qt.gtimg.cn");
        HttpResponse rsq=null;
        try {
            rsq=httpClient.execute(host,req);
        } catch (IOException e) {
            e.printStackTrace();
            retstr="";
            return retstr;
        }

        try {
            //System.out.println(IOUtils.toString(rsq.getEntity().getContent(),"GBK"));
            String gbk=IOUtils.toString(rsq.getEntity().getContent(),"GBK");
            String utf8=new String(gbk.getBytes("GBK"),"UTF-8");
            retstr=utf8;
           // System.out.println(utf8);

        } catch (IOException e) {
            e.printStackTrace();
            retstr="";
        }
        return retstr;
    }
    public int getCurTradeDay()
    {
        String retStr=fetchHttp("000001");
        String[] array = retStr.split("~");
        return Integer.valueOf(array[30].substring(0,8));

    }

    public void test(){
        System.out.println("DDDDDDDD");
    }
}
